from django.shortcuts import render,redirect
import hashlib
from Store.models import *

def setPassword(password):
    md5 = hashlib.md5()
    md5.update(password.encode())
    result = md5.hexdigest()
    return result

def index(request):
    return render(request,"store/index.html")

def login(request):
    return render(request,"store/login.html")

def register(request):
    if request.method == "POST":
        # 注册
        username = request.POST.get("username")
        password = request.POST.get("password")
        password_again = request.POST.get("password_again")

        not_empty = username and password and password_again
        same_password = password == password_again
        if not_empty and same_password:
            user = User()
            user.user_name = username
            user.password = setPassword(password)
            user.save()
            return redirect("/Store/login")     # redirect（）跳转页面
    return render(request,"store/register.html")


